import { createSlice, createAsyncThunk } from '@reduxjs/toolkit'

export const asyncCount = createAsyncThunk('yyy', (num: number) => {
  // 注意: 默认Promise成功的值的类型是unknow类型.但是我们成功之后要传递的数据是number类型.number类型不能赋值给unknow类型.所以通过promise的泛型,告诉ts,未来promise成功的值是number类型
  return new Promise<number>((resolve) => {
    setTimeout(() => {
      resolve(num)
    }, 1000)
  })
})

export default createSlice({
  name: 'test',
  initialState: { c: 1, xxx: 123 },
  reducers: {
    add(state, action) {
      state.c = state.c + action.payload
    },
  },
  extraReducers(builder) {
    builder.addCase(asyncCount.fulfilled, (state, action) => {
      state.c = state.c + action.payload
    })
  },
})
